/**
 * 水果组件
 */
class fruit extends Frame.Display.EuiView {
  public statusLbl: eui.Label;
  public shapeRect: eui.Rect;
  phyShape: p2.Shape;
  type = "fruit";
  public fruitImg: eui.Image;

  //已经合成过
  combined: boolean = false;

  data = {
    mass: 10,
    position: [-100, -100],
    radius: 50,
    status: 1
  };
  public phyBody: p2.Body;

  constructor({ mass = 10, position = [-100, -100], radius = 50, status = 0 }) {
    super("resource/game_skins/gameScene/fruitSkin.exml");
    this.data.mass = mass;
    this.data.position = position;
    this.data.radius = radius;
    this.data.status = status;
  }
  init() {
    this.statusLbl.text = this.data.status.toString();
    this.width = this.data.radius * 2;
    this.height = this.data.radius * 2;
    this.shapeRect.ellipseHeight = this.width;
    this.shapeRect.ellipseWidth = this.width;
    this.x = this.data.position[0];
    this.y = this.data.position[1];

    this.anchorOffsetX = this.width / 2;
    this.anchorOffsetY = this.height / 2;

    this.fruitImg.source = RES.getRes(`fruit-${this.data.status}_png`);
  }
  setPhysicsBody(radius: number) {

    let phy = physicsUtil.getInstance();
    let data = this.data;
    let [rX, rY] = phy.posEgretToP2(this.data.position[0], this.data.position[1]);
    let phyR = phy.lengthEgretToP2(radius);
    this.phyShape = new p2.Circle({ radius: phyR, color: 0xffffff });
    this.phyBody = new p2.Body({ mass: data.mass, position: [rX, rY], allowSleep: false });

    this.phyBody.addShape(this.phyShape);
    this.phyBody.displays = [this];

    this.phyBody.damping =3e-1;
    this.phyBody.angularDamping = 3e-1;
    this.phyBody.type = p2.Body.DYNAMIC;

    this.anchorOffsetX = this.width / 2;
    this.anchorOffsetY = this.height / 2;

    physicsUtil.getInstance().phyWorld.addBody(this.phyBody);


  }

  //设置位置（显示位置和物理位置）
  setPos(x, y) {
    this.data.position = [x, y];
  }
  scaleDisplayToNormal($radius, time) {
    egret.Tween.get(this).to({
      anchorOffsetX: $radius,
      anchorOffsetY: $radius,
      width: $radius * 2,
      height: $radius * 2
    }, time);
    this.data.radius = $radius;

  }

}